package cs2340.droidceptiontodo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

/**
 * Filter Activity Class
 * Filters by each option
 * @author Susmita
 *
 */
public class FilterActivity extends Activity implements OnCheckedChangeListener, OnClickListener {
	CheckBox date;
	CheckBox category;
	CheckBox incomplete;
	CheckBox mainOptionRG;
	Button clear;
	Button cancel;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.filter);
		//mainOptionRG = (RadioGroup)findViewById(R.id.radioGroupMain);
		clear = (Button)findViewById(R.id.clear);
		cancel = (Button)findViewById(R.id.cancelfilter);
		date = (CheckBox)findViewById(R.id.date);
		category = (CheckBox)findViewById(R.id.category);
		incomplete=(CheckBox)findViewById(R.id.incomplete);
		
		//Database settingsManager = new Database(this);
		Database settingsManager = Database.getInstance(this);
		settingsManager.open();
		String filterByCategory = settingsManager.getSetting("filterByCategory");
		String filterByChecked = settingsManager.getSetting("filterByChecked");
		String sortByDate = settingsManager.getSetting("sortByDate");
		settingsManager.close();
		
		if(filterByCategory != null && !filterByCategory.equals("null")) {
			category.setChecked(true);
		}
		if(filterByChecked != null && !filterByChecked.equals("null")) {
			incomplete.setChecked(true);
		}
		if(sortByDate != null && !sortByDate.equals("null")) {
			date.setChecked(true);
		}
		
		//mainOptionRG.setOnCheckedChangeListener(this);
		clear.setOnClickListener(this);
		cancel.setOnClickListener(this);
		category.setOnCheckedChangeListener(this);
		incomplete.setOnCheckedChangeListener(this);
		date.setOnCheckedChangeListener(this);
	}
	
	public void OnRestoreInstanceState(Bundle icicle) {
		onCreate(icicle);
	}
	
	/** 
	 * Checks to see which filter option the user selects
	 * Changes the screen accordingly for date and category options
	 * Returns simple success message for incomplete option
	 * 
	 */
	/*@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		if (date.isChecked()){
			date.setChecked(true);
			Intent i = new Intent();                 
    		i.setClassName("cs2340.droidceptiontodo", "cs2340.droidceptiontodo.DateActivity");  
    		startActivity(i);
    		finish();
		}
		else if (category.isChecked()){
			Intent i = new Intent();                 
    		i.setClassName("cs2340.droidceptiontodo", "cs2340.droidceptiontodo.CategoryActivity");  
    		startActivity(i);
    		finish();
		}
		else if (incomplete.isChecked()){
			//Database settingsManager = new Database(this);
			Database settingsManager = Database.getInstance(this);
			settingsManager.open();
			settingsManager.setSetting("filterByChecked", "false");
			settingsManager.close();
			finish();
		}
	}*/
	/** 
	 * If user clicks on clear button, unchecks any filter option selection
	 * If user clicks on cancel button, returns back to List of To-Do items screen
	 * 
	 */

	@Override
	public void onClick(View v) {
		if (v.getId()==clear.getId()){
			//Database settingsManager = new Database(this);
			Database settingsManager = Database.getInstance(this);
			settingsManager.open();
			settingsManager.setSetting("sortByDate", "null");
			settingsManager.setSetting("filterByCategory", "null");
			settingsManager.setSetting("filterByChecked", "null");
			date.setChecked(false);
			category.setChecked(false);
			incomplete.setChecked(false);
			settingsManager.close();
			finish();
		}
		else if(v.getId()==cancel.getId()){
			finish();
			
		}
		
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if (date.isChecked()){
			date.setChecked(true);
			Intent i = new Intent();                 
    		i.setClassName("cs2340.droidceptiontodo", "cs2340.droidceptiontodo.DateActivity");  
    		startActivity(i);
		}
		if (!date.isChecked()) {
			Database settingsManager = Database.getInstance(this);
			settingsManager.open();
			settingsManager.setSetting("sortByDate", "null");
			settingsManager.close();
		}
		if (category.isChecked()){
			Intent i = new Intent();                 
    		i.setClassName("cs2340.droidceptiontodo", "cs2340.droidceptiontodo.CategoryActivity");  
    		startActivity(i);
		}
		if (!category.isChecked()) {
			Database settingsManager = Database.getInstance(this);
			settingsManager.open();
			settingsManager.setSetting("filterByCategory", "null");
			settingsManager.close();
		}
		if (incomplete.isChecked()){
			//Database settingsManager = new Database(this);
			Database settingsManager = Database.getInstance(this);
			settingsManager.open();
			settingsManager.setSetting("filterByChecked", "false");
			settingsManager.close();
		}
		if (!incomplete.isChecked()) {
			Database settingsManager = Database.getInstance(this);
			settingsManager.open();
			settingsManager.setSetting("filterByChecked", "null");
			settingsManager.close();
		}
	}
	
	 
}
